package com.pet.shop.controller.pet;

import com.alibaba.fastjson.JSONObject;
import com.pet.config.Constants;
import com.pet.config.SessionConstants;
import com.pet.shop.entity.shop.PetInfo;
import com.pet.shop.entity.shop.PetRelay;
import com.pet.shop.entity.user.UserInfo;
import com.pet.shop.service.pet.IPetInfoService;
import com.pet.shop.service.pet.IPetRelayService;
import com.pet.shop.utils.ApiResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/api/pet")
public class PetApiController {

    @Autowired
    private IPetInfoService petInfoService;
    @Autowired
    private IPetRelayService petRelayService;

    @RequestMapping("get")
    public Map<String, Object> petInfoList(Long petId){
        return ApiResultUtil.success(petInfoService.getEntity(petId));
    }

    /**
     * 宠物列表
     * @return
     */
    @RequestMapping("petInfoList")
    public Map<String, Object> petInfoList(HttpServletRequest request){
        Map<String,Object> map = new HashMap<>();
        UserInfo userInfo = (UserInfo) request.getSession().getAttribute(SessionConstants.APP_SESSION_KEY);
        map.put("userId",userInfo.getId());
        List<PetInfo> petInfoList = petInfoService.queryForPetList(map);
        return ApiResultUtil.success(petInfoList);
    }

    /**
     * 宠物列表
     * @return
     */
    @RequestMapping("insertPetInfo")
    public Map<String, Object> insertPetInfo(HttpServletRequest request,String petInfoStr){
        if(null == petInfoStr){
            return ApiResultUtil.error_param("参数错误，请重试！");
        }
        PetInfo petInfo = JSONObject.parseObject(petInfoStr,PetInfo.class);
        UserInfo userInfo = (UserInfo) request.getSession().getAttribute(SessionConstants.APP_SESSION_KEY);
        petInfo.setUserId(userInfo.getId());
        petInfo.setCreateTime(new Date());
        petInfo.setCreatorId(userInfo.getId());
        petInfoService.insertEntity(petInfo);
        return ApiResultUtil.success(petInfo);
    }

    /**
     * 更新宠物信息
     * @return
     */
    @RequestMapping("updatePetInfo")
    public Map<String, Object> updatePetInfo(HttpServletRequest request,String petInfoStr){
        if(null == petInfoStr){
            return ApiResultUtil.error_param("参数错误，请重试！");
        }
        UserInfo userInfo = (UserInfo) request.getSession().getAttribute(SessionConstants.APP_SESSION_KEY);
        PetInfo petInfo = JSONObject.parseObject(petInfoStr,PetInfo.class);
        petInfo.setModifyTime(new Date());
        petInfo.setModifierId(userInfo.getId());
        petInfoService.updateEntity(petInfo);
        return ApiResultUtil.success(petInfo);
    }

    /**
     * 宠物打针、除虫、等记录接口
     * @param request
     * @param type  根据不同的类型查找对应的记录
     * @return
     */
    @RequestMapping("actInfoList")
    public Map<String, Object> actInfoList(HttpServletRequest request,Integer type, Long petId){
        if(null == type || null == petId){
            return ApiResultUtil.error_param("参数错误，请重试");
        }
        if(Constants.ACT_CHULIANG.equals(type)){ //冲凉
            return ApiResultUtil.success(petInfoService.queryForShowerList(petId));
        }else if(Constants.ACT_DAZHENG.equals(type)){ //打针记录
            return ApiResultUtil.success(petInfoService.queryForInjectionList(petId));
        }else{                                        //
            return ApiResultUtil.success(petInfoService.queryForPetDisinsecList(petId));
        }
    }

    /**
     * 我的预约
     * @param request
     * @return
     */
    @RequestMapping("petRelayList")
    public Map<String, Object> petRelayList(HttpServletRequest request){
        UserInfo userInfo = (UserInfo) request.getSession().getAttribute(SessionConstants.APP_SESSION_KEY);
        List<PetRelay> relayList = petInfoService.queryForPetRelationListByUserId(userInfo.getId());
        if(null != relayList && !relayList.isEmpty()){
            List<Long> petIdList = new ArrayList<>();
            for(PetRelay petRelay:relayList){
                petIdList.add(petRelay.getPetId());
            }
            Map<String,Object> map = new HashMap<>();
            map.put("petIdList",petIdList);
            List<PetInfo> petInfoList = petInfoService.queryForPetList(map);
            if(null != petInfoList && !petInfoList.isEmpty()){
                Map<Long,String> petMap = new HashMap<>();
                for(PetInfo petInfo:petInfoList){
                    petMap.put(petInfo.getId(),petInfo.getAvatar());
                }
                for(PetRelay petRelay:relayList){
                    petRelay.setPath(petMap.get(petRelay.getPetId()));
                }
            }
        }

        return ApiResultUtil.success(relayList);
    }

    /**
     * 添加预约
     * @param request
     * @return
     */
    @RequestMapping("insertPetRelayInfo")
    public Map<String, Object> insertPetRelayInfo(HttpServletRequest request,String petRelayStr){
        if(StringUtils.isEmpty(petRelayStr)){
            return ApiResultUtil.error_param("添加预约失败！请重试");
        }
        UserInfo userInfo = (UserInfo) request.getSession().getAttribute(SessionConstants.APP_SESSION_KEY);
        PetRelay petRelay = JSONObject.parseObject(petRelayStr,PetRelay.class);
        petRelay.setCreateTime(new Date());
        petRelay.setCreatorId(userInfo.getId());
        petRelay.setUserId(userInfo.getId());
        petRelay.setUserName(userInfo.getNickname());
        return ApiResultUtil.success(petInfoService.insertRelayInfo(petRelay));
    }

    /**
     * 用户详情
     * @param request
     * @return
     */
    @RequestMapping("userInfo")
    public Map<String, Object> userInfo(HttpServletRequest request,PetRelay petRelay){
        UserInfo userInfo = (UserInfo) request.getSession().getAttribute(SessionConstants.APP_SESSION_KEY);
        return ApiResultUtil.success(userInfo);
    }

    /**
     *
     * @param request
     * @param date
     * @return
     */
    @RequestMapping("checkRelay")
    public Map<String, Object> checkRelay(HttpServletRequest request, String date){
        if(StringUtils.isEmpty(date)){
            return ApiResultUtil.error_param("日期必选 yyyy-MM-dd");
        }
        Date relayDate = null;
        try {
            relayDate = new SimpleDateFormat("yyyy-MM-dd").parse(date);
        } catch (ParseException e) {
            return ApiResultUtil.error_param("日期格式不对 yyyy-MM-dd");
        }
        return ApiResultUtil.success(petRelayService.checkRelayDate(relayDate));
    }
}
